package com.work.straighttalk.model;

import android.util.Log;

import com.work.straighttalk.bean.Notifyfriends;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.bean.User;
import com.work.straighttalk.model.i.NewFriendsMI;
import com.work.straighttalk.model.i.OnNewFriendsListener;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by dllo on 16/3/7.
 */
public class NewFriendsMImpl implements NewFriendsMI {

    @Override
    public void verifications(final OnNewFriendsListener listener) {

        User user = BmobUser.getCurrentUser(StraightTalk.applicationContext, User.class);
        BmobQuery<Notifyfriends> query = new BmobQuery<Notifyfriends>();
        query.addWhereEqualTo("user", user);
        query.findObjects(StraightTalk.applicationContext, new FindListener<Notifyfriends>() {
            @Override
            public void onSuccess(List<Notifyfriends> list) {

                listener.getNewFriendsList(list);

            }

            @Override
            public void onError(int i, String s) {
            }
        });
    }

    @Override
    public void verifications(final String username, final OnNewFriendsListener listener) {

        Log.d("NewFriendsMImpl", username);
        User user = BmobUser.getCurrentUser(StraightTalk.applicationContext, User.class);
        BmobQuery<Notifyfriends> query = new BmobQuery<Notifyfriends>();
        query.addWhereEqualTo("user", user);
        query.findObjects(StraightTalk.applicationContext, new FindListener<Notifyfriends>() {
            @Override
            public void onSuccess(List<Notifyfriends> list) {
                Log.d("NewFriendsMImpl", "list.size():" + list.size());
                boolean flag = false;
                boolean one = false;
                if (list.size() == 0) {
                    flag = true;
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getName().equals(username)) {
                            one = true;
                            return;
                        }
                    }
                }
                if (!one) {
                    isHasUsernameAddName(username);
                    Log.d("NewFriendsMImpl564", username);

                    User user = BmobUser.getCurrentUser(StraightTalk.applicationContext, User.class);
                    BmobQuery<Notifyfriends> query = new BmobQuery<Notifyfriends>();
                    query.addWhereEqualTo("user", user);
                    query.findObjects(StraightTalk.applicationContext, new FindListener<Notifyfriends>() {

                        @Override
                        public void onSuccess(List<Notifyfriends> list) {
                            listener.getNewFriendsList(list);
                        }

                        @Override
                        public void onError(int i, String s) {

                        }
                    });
                    Log.d("NewFriendsasdasdMImpl", "list.size():" + list.size());
                    one = false;
                }
                if (flag) {
                    isHasUsernameAddName(username);
                    flag = false;
                }
            }

            @Override
            public void onError(int i, String s) {
                Log.i("65156165as1da", "65a1sd65sa1d65as1d");
            }
        });

    }

    public void isHasUsernameAddName(String username) {
        User user = BmobUser.getCurrentUser(StraightTalk.applicationContext, User.class);
        Notifyfriends no = new Notifyfriends();
        no.setName(username);
        no.setUser(user);

        no.save(StraightTalk.applicationContext, new SaveListener() {
            @Override
            public void onSuccess() {
                Log.d("MyContactListener", "保存成功");
            }

            @Override
            public void onFailure(int i, String s) {

            }
        });

    }


}
